package Math;

import java.util.Arrays;

public class _628_MaximumProductofThreeNumbers {
    //wrong answer,the problem doesn't require consequence
    public int maximumProduct_w1(int[] nums) {
        if (nums.length < 3) {
            return 0;
        }
        int max = Integer.MIN_VALUE;
        for(int i=1;i<nums.length-1;i++) {
            int tempProduct = nums[i] * nums[i - 1] * nums[i + 1];
            max = Math.max(tempProduct, max);
        }
        return max;
    }
    //test case:[-4,-3,0,1,2,40]:max is 480
    public int maximumProduct(int[] nums) {
        if (nums.length < 3) {
            return 0;
        }
        Arrays.sort(nums);
        //max product from the three max numbers or the one max number and two min number
        return Math.max(nums[0]*nums[1]*nums[nums.length-1],nums[nums.length-3]*nums[nums.length-2]*nums[nums.length-1]);
    }
}
